public class num203 {
    public ListNode removeElements(ListNode head, int val) {
        while(head != null && head.val == val){
            ListNode prev = head;
            head = head.next;
            prev.next = null;
        }
        if (head == null){
            return null;
        }
        ListNode x = head;
        while (x.next != null){
            if(x.next.val == val){
                ListNode node = x.next;
                x.next = node.next;
                node.next = null;
            }else{
                x = x.next;
            }
        }
        return head;
    }
}
